<template>
	<view class="search">
		<view class="top">
			<uni-icons style="margin-left: 25rpx;" type="left" size="25" color="#b8b8b8" @click="toBackPage" ></uni-icons>
			<view class="input">
				<uni-icons type="search" size="25" color="#b8b8b8" ></uni-icons>
				<input @input="flushData" style="width:530rpx" placeholder="请输入" v-model="queryInput.keyword" />
			</view>
		</view>
		
		<view class="types">
			<view v-for="(item,index) in types">
				<view @click="changeTab(index)" class="type"
					:class="index == queryInput.type ? 'type-current': '' ">
					<view>{{item}}</view>
					<!-- <view v-if="index == currentTypeIndex" class="type-line"></view> -->
				</view>
			</view>
		</view>
		
		<view class="content">
			
			
			<view v-if="queryInput.type == 0" class="islands">
				<view v-if="islands.length===0 && posts.length===0" style="padding: 30rpx;text-align: center;font-size: 34rpx;">
					暂无浪屿
				</view>
				<view class="island" v-for="(item,index) in islands" @click="toIslandDetail(item.joined,item.type,item.id)">
					<view class="avatar">
						<image mode="aspectFill" :src="item.background"></image>
					</view>
					<view class="island-info">
						<view class="name">{{item.name}}</view>
						<view class="description">{{item.description}}</view>
					</view>
				</view>
			</view>
			
			<view v-if="queryInput.type == 1" class="courses">
				<view v-if="courses.length===0 && posts.length===0" style="padding: 30rpx;text-align: center;font-size: 34rpx;">
					暂无知桨
				</view>
				<view class="course" v-for="(item,index) in courses"  @click="toCourseDetail(item.id)">
					<view class="avatar">
						<image mode="aspectFill" :src="item.cover"></image>
					</view>
					<view class="course-info">
						<view class="name">{{item.name}}</view>
						<view class="description">{{item.description}}</view>
					</view>
				</view>
			</view>
			
			<view v-if="queryInput.type == 2" class="questions">
				<view v-if="questions.length===0" style="padding: 30rpx;text-align: center;font-size: 34rpx;">
					暂无问题
				</view>
				<view class="question" v-for="(item,index) in questions"  @click="toQuestionDetail(item.id)">
					<view class="title">{{index+1}} 、{{item.title}}</view>
					<view class="context">{{getSimpleText(item.context)}}</view>
				</view>
			</view>
			
			<view v-if="queryInput.type == 3" class="posts">
				<view v-if="posts.length===0" style="padding: 30rpx;text-align: center;font-size: 34rpx;">
					暂无帖子
				</view>
				<view class="post" v-for="(item,index) in posts" >
					<view class="post-top">
						<view style="display: flex;flex-direction: row;align-items: center;">
							<view class="avatar">
								<image mode="aspectFill" :src="item.avatar"></image>
							</view>
							<view style="margin-left: 20rpx;">{{item.nickname}}</view>
						</view>
						<view class="date">昨天8:03</view>
					</view>
					<view class="post-content">{{item.content}}</view>
				</view>
			</view>
			
			<view v-if="queryInput.type == 4" class="messages">
				<view v-if="messages.length===0" style="padding: 30rpx;text-align: center;font-size: 34rpx;">
					暂无私信
				</view>
				<view class="message" v-for="(item,index) in messages" >
					<view class="message-top">
						<view style="display: flex;flex-direction: row;align-items: center;">
							<view class="avatar">
								<image mode="aspectFill" :src="item.avatar"></image>
							</view>
							<view style="margin-left: 20rpx;">{{item.nickname}}</view>
						</view>
						<view class="date">昨天8:03</view>
					</view>
					<view class="message-content">{{item.content}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchInput: '',
				types:["浪屿","知桨","问题","帖子","私信"],
				
				queryInput:{
					keyword:"",
					pageNow:1,
					pageSize:20,
					total:0,
					type:0
				},
				
				islands: [],
				
				courses: [],
				
				questions: [],
				
				posts: [
					{
						id: "",
						content: "近几年青少年抑郁者迅速增多，许多悲观论调也流传甚广，不知道为什么，我从最初会心，我从最初会",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: false,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: false,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: true,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: true,
						date: null
					}
				],
				
				
				
				
				
				
				messages: [
					{
						id: "",
						content: "近几年青少年抑郁者迅速增多，许多悲观论调也流传甚广，不知道为什么，我从最初会心，我从最初会",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: false,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: false,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: true,
						date: null
					},
					{
						id: "",
						content: "第三章第六节的内容补充完善",
						nickname: "带着脑子去流浪",
						avatar: "https://ekin-1010.oss-cn-beijing.aliyuncs.com/avatar.png",
						isReaded: true,
						date: null
					}
				],
			}
		},
		onLoad(options) {
			this.queryInput.type = options.type
			if(options.type == 0){
				this.pageIsland()
			}
		},
		methods: {
			pageIsland(){
				this.queryInput.name = this.queryInput.keyword
				this.$api.pageIsland(this.queryInput).then(res => {
					this.islands = res.data.rows
					this.queryInput.total = res.data.total
				})
			},
			pageCourse(){
				this.queryInput.name = this.queryInput.keyword
				this.$api.pageCourse(this.queryInput).then(res => {
					this.courses = res.data.rows
					this.queryInput.total = res.data.total
				})
			},
			pageQuestion(){
				this.queryInput.title = this.queryInput.keyword
				this.$api.pageQuestion(this.queryInput).then(res => {
					this.questions = res.data.rows
					this.queryInput.total = res.data.total
				})
			},
			
			
			flushData(){
				if(this.queryInput.type == 0){
					this.queryInput.pageNow = 1
					this.pageIsland()
				}
				if(this.queryInput.type == 1){
					this.queryInput.pageNow = 1
					this.pageCourse()
				}
				if(this.queryInput.type == 2){
					this.queryInput.pageNow = 1
					this.pageQuestion()
				}
			},
			
			toIslandDetail(joined,type,islandId){
				if(joined){
					if (type == 0) {
						uni.navigateTo({
							url: '/pages_being/island/memory-island-detail?islandId=' + islandId
						})
					}
					if (type == 1) {
						uni.navigateTo({
							url: '/pages_being/island/post-island-detail?islandId=' + islandId
						})
					}
					if (type == 2) {
						uni.navigateTo({
							url: '/pages_being/island/article-island-detail?islandId=' + islandId
						})
					}
				}else{
					uni.navigateTo({
						url: '/pages_being/island/island-detail?islandId=' + islandId
					})
				}
			},
			
			toCourseDetail(courseId){
				uni.navigateTo({
					url: '/pages_being/course/course-detail?courseId=' + courseId
				})
			},
			
			toQuestionDetail(questionId){
				uni.navigateTo({
					url: '/pages_modex/question/question-detail?questionId=' + questionId,
				});
			},
			
			
			changeTab(index) {
				this.queryInput.type = index
				this.queryInput.pageNow = 1
				
				if(this.queryInput.type == 0){
					this.pageIsland()
				}
				if(this.queryInput.type == 1){
					this.pageCourse()
				}
				if(this.queryInput.type == 2){
					this.pageQuestion()
				}
			},
			toBackPage(){
				uni.navigateBack()
			},
			getSimpleText(html) {
				var re1 = new RegExp("<.+?>", "g"); //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
				var msg = html.replace(re1, ''); //执行替换成空字符
				return msg;
			},
		}
	}
</script>

<style scoped lang="scss">
	.search{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 80rpx;
		
		.top{
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			
			padding: 20rpx 0;
			
			background-color: white;
			border-bottom: solid thin #efefef;
			
			
			.input{
				height: 80rpx;
				width: 600rpx;
				background-color: #f3f3f3;
				border-radius: 50rpx;
				
				padding-left: 20rpx;
				margin-right: 25rpx;
				
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				
				
			}
			
			
		}
		
		.types {
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-top: 40rpx;
			width: 100%;
		
			.type {
				margin-left: 40rpx;
				color: gray;
				height: 65rpx;
		
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;
			}
		
			.type-current {
				color: #2c2c2c;
				font-weight: bold;
			}
		
			.type-line {
				width: 40rpx;
				height: 7rpx;
				border-radius: 5rpx;
				background-color: #2c2c2c;
			}
		
		
		}
		
		.content{
			
			display: flex;
			flex-direction: column;
			padding-bottom: 100rpx;
			
			.islands{
				display: flex;
				flex-direction: column;
				width: 100%;
				
				.island{
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					margin-top: 20rpx;
					
					.avatar{
						height: 100rpx;
						width: 100rpx;
					}
					
					.island-info{
						width: 570rpx;
						margin-left: 20rpx;
						
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						
						.name{
							
						}
						
						.description{
							font-size: 28rpx;
							color: #cfcfcf;
							/*超过范围进行隐藏不显示*/
							overflow: hidden; 
							/*文本溢出, 使用省略号 */
							text-overflow:ellipsis;
							/*连续的空白符会被合并,但文本内的换行无效*/
							white-space: nowrap;
						}
					}
					
				}
				
			}
			
			.courses{
				display: flex;
				flex-direction: column;
				width: 100%;
				
				.course{
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					margin-top: 20rpx;
					
					.avatar{
						height: 100rpx;
						width: 100rpx;
					}
					
					.course-info{
						width: 570rpx;
						margin-left: 20rpx;
						
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						
						.name{
							
						}
						
						.description{
							font-size: 28rpx;
							color: #cfcfcf;
							/*超过范围进行隐藏不显示*/
							overflow: hidden; 
							/*文本溢出, 使用省略号 */
							text-overflow:ellipsis;
							/*连续的空白符会被合并,但文本内的换行无效*/
							white-space: nowrap;
						}
					}
					
				}
				
			}
			
			.questions{
				display: flex;
				flex-direction: column;
				width: 100%;
				
				.question{
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					margin-top: 20rpx;
					margin: 20rpx 30rpx 0 30rpx;
				
					
					.title{
						
					}
					
					.context{
						margin-top: 10rpx;
						font-size: 28rpx;
						color: #acacac;
						
						/*超过范围进行隐藏不显示*/
						overflow: hidden;
						/*文本溢出, 使用省略号 */
						text-overflow: ellipsis;
						/* 以webkit盒子展示, 比较适用于webkit内核和移动端*/
						display: -webkit-box;
						/*限制块容器显示的文本行数, 按照你的需求设置*/
						-webkit-line-clamp: 2;
						/*盒子的方向, 垂直、水平等*/
						-webkit-box-orient: vertical;
						/*文字换行, 可以在单词内断*/
						word-wrap: break-word;
					}
					
				}
				
			}
			
			
			.posts {
				display: flex;
				flex-direction: column;
				margin-left: 20px;
				margin-right: 20px;
			
			
				.post {
					display: flex;
					flex-direction: column;
					margin-top: 20rpx;
			
					.post-top {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						align-items: flex-start;
			
						.avatar {
							height: 60rpx;
							width: 60rpx;
							border-radius: 50%;
							overflow: hidden;
						}
			
						.date {
							font-size: 28rpx;
							color: gray;
						}
			
					}
			
					.post-content {
						width: 590rpx;
						margin-left: 80rpx;
						font-size: 28rpx;
						color: gray;
			
						/*超过范围进行隐藏不显示*/
						overflow: hidden;
						/*文本溢出, 使用省略号 */
						text-overflow: ellipsis;
						/* 以webkit盒子展示, 比较适用于webkit内核和移动端*/
						display: -webkit-box;
						/*限制块容器显示的文本行数, 按照你的需求设置*/
						-webkit-line-clamp: 1;
						/*盒子的方向, 垂直、水平等*/
						-webkit-box-orient: vertical;
						/*文字换行, 可以在单词内断*/
						word-wrap: break-word;
					}
				}
			}
			
			.messages {
				display: flex;
				flex-direction: column;
				margin-left: 20px;
				margin-right: 20px;
			
			
				.message {
					display: flex;
					flex-direction: column;
					margin-top: 20rpx;
			
					.message-top {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						align-items: flex-start;
			
						.avatar {
							height: 60rpx;
							width: 60rpx;
							border-radius: 50%;
							overflow: hidden;
						}
			
						.date {
							font-size: 28rpx;
							color: gray;
						}
			
					}
			
					.message-content {
						width: 590rpx;
						margin-left: 80rpx;
						font-size: 28rpx;
						color: gray;
			
						/*超过范围进行隐藏不显示*/
						overflow: hidden;
						/*文本溢出, 使用省略号 */
						text-overflow: ellipsis;
						/* 以webkit盒子展示, 比较适用于webkit内核和移动端*/
						display: -webkit-box;
						/*限制块容器显示的文本行数, 按照你的需求设置*/
						-webkit-line-clamp: 1;
						/*盒子的方向, 垂直、水平等*/
						-webkit-box-orient: vertical;
						/*文字换行, 可以在单词内断*/
						word-wrap: break-word;
					}
				}
			}
			
		}
		
		image{
			height: 100%;
			width: 100%;
		}
	}
</style>
